﻿using OpenAiClient.Events;
using OpenAiClient.Services;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;

namespace OpenAiClient.ViewModels
{
    internal class SettingViewModel : BindableBase
    {
        public DelegateCommand SaveServerConfigCmd { get; }
        public DelegateCommand SaveSdkConfigCmd { get; }

        public string ServerIp
        {
            get => _serverIp;
            set => SetProperty(ref _serverIp, value);
        }

        public string ServerPortString
        {
            get => _serverPortString;
            set => SetProperty(ref _serverPortString, value);
        }

        public string RequestTimeoutString
        {
            get => _requestTimeoutString;
            set => SetProperty(ref _requestTimeoutString, value);
        }

        public string ApiKey
        {
            get => _apiKey;
            set => SetProperty(ref _apiKey, value);
        }

        private string _serverIp = string.Empty;
        private string _serverPortString = string.Empty;
        private string _requestTimeoutString = string.Empty;
        private string _apiKey = string.Empty;

        private readonly IConfigService _configService;
        private readonly IEventAggregator _eventAggregator;

        public SettingViewModel(
            IConfigService configService,
            IEventAggregator eventAggregator
        )
        {
            _configService = configService;
            _eventAggregator = eventAggregator;

            SaveServerConfigCmd = new DelegateCommand(OnSaveServerConfig);
            SaveSdkConfigCmd = new DelegateCommand(OnSaveSdkConfig);

            ServerIp = _configService.ServerIp;
            ServerPortString = $"{_configService.ServerPort}";
            RequestTimeoutString = $"{_configService.RequestTimeout}";
            ApiKey = _configService.ApiKey;
        }

        private void OnSaveServerConfig()
        {
            var serverIp = ServerIp.Trim();
            if (string.IsNullOrWhiteSpace(serverIp))
            {
                _eventAggregator.GetEvent<ShowGrowlWarningEvent>().Publish("请输入IP");
                return;
            }

            var serverPortString = ServerPortString.Trim();
            if (string.IsNullOrWhiteSpace(serverPortString))
            {
                _eventAggregator.GetEvent<ShowGrowlWarningEvent>().Publish("请输入端口");
                return;
            }

            var requestTimeoutString = RequestTimeoutString.Trim();
            if (string.IsNullOrWhiteSpace(requestTimeoutString))
            {
                _eventAggregator.GetEvent<ShowGrowlWarningEvent>().Publish("请输入请求超时");
                return;
            }

            if (!int.TryParse(serverPortString, out var serverPort) || serverPort <= 0)
            {
                _eventAggregator.GetEvent<ShowGrowlWarningEvent>().Publish("请检查端口");
                return;
            }

            if (!int.TryParse(requestTimeoutString, out var requestTimeout) || requestTimeout < 0)
            {
                _eventAggregator.GetEvent<ShowGrowlWarningEvent>().Publish("请检查请求超时");
                return;
            }

            if (serverIp == _configService.ServerIp &&
                serverPort == _configService.ServerPort &&
                requestTimeout == _configService.RequestTimeout)
            {
                return;
            }

            _configService.SetServerPara(serverIp, serverPort, requestTimeout);
            _eventAggregator.GetEvent<ShowGrowlSuccessEvent>().Publish("服务器设置已更新");
        }

        private void OnSaveSdkConfig()
        {
            var apiKey = ApiKey.Trim();
            if (apiKey == _configService.ApiKey)
            {
                return;
            }

            _configService.SetSdkPara(apiKey);
            _eventAggregator.GetEvent<ShowGrowlSuccessEvent>().Publish("SDK设置已更新");
        }
    }
}
